package me.zhengjie.modules.website.domain;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotBlank;
import lombok.Data;
import me.zhengjie.base.BaseEntity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

import javax.persistence.*;

/**
 *
 *
 * @author system
 * @since 2024-09-26 16:41:54
 */
@Entity
@Data
@Table(name = "content_table" )
@ApiModel(value = "ContentTable", description = " ")
public class ContentTable extends BaseEntity implements Serializable{

    private static final long serialVersionUID=1L;

    /**
     *
     */
    @Column(name = "content_id" )
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long contentId;

    /**
     *
     */
    @Column(name = "title" )
    @NotBlank(message = "【 】不能为空")
    private String title;

    /**
     *
     */
    @Column(name = "subtitle" )
    @NotBlank(message = "【 】不能为空")
    private String subtitle;

    /**
     *
     */
    @Column(name = "body" ,columnDefinition = "TEXT")
    private String body;

    /**
     *
     */
    @Column(name = "cover_image" )
    private String coverImage;

    /**
     *
     */
    @Column(name = "link_url" )
    private String linkUrl;

    /**
     *
     */
    @Column(name = "is_downloadable" )
    private Boolean isDownloadable;

    /**
     *  1
     */
    @Column(name = "status" )
    @ApiModelProperty(value = "内容状态：1-发布、2-待发布", example = "1", required = true)
    private Integer status;


    @Column(name = "column_ids")
    @ApiModelProperty(value = "栏目ID列表")
    private String columnIds; // 用逗号分隔的栏目ID列表

    /**
     *
     */
    @Column(name = "attachment_path" ,columnDefinition = "TEXT")
    private String attachmentPath;

    /**
     *
     */
    @Column(name = "view_count" )
    private Integer viewCount;

    /**
     *
     */
    @Column(name = "like_count" )
    private Integer likeCount;

    /**
     *
     */
    @Column(name = "is_pinned" )
    private Boolean isPinned;

    /**
     * 排序
     */
    @Column(name = "sort_order")
    private Integer sortOrder;

    @Column(name = "support_download")
    @ApiModelProperty(value = "是否支持下载")
    private Boolean supportDownload;

    @Column(name = "file_type")
    @ApiModelProperty(value = "文件类型 1-文档 2-图片 3-视频 4-其他")
    private String fileType;
    /**
     *
     */
    @Column(name = "release_time" )
    @ApiModelProperty(value = "发布时间")
    private Date releaseTime;

    @Transient
    private List<ColumnTable> columnTables;

    @Transient
    private ColumnTable columnTable;

    @Transient
    private List<Long> columnIdsList;

    // 在加载实体时，将数据库的逗号分隔的字符串转换为 List<Long>
    @PostLoad
    public void postLoad() {
        if (columnIds != null && !columnIds.isEmpty()) {
            this.columnIdsList = Arrays.stream(columnIds.split(","))
                    .map(Long::valueOf)  // 转换每个字符串为 Long
                    .collect(Collectors.toList());
        } else {
            this.columnIdsList = new ArrayList<>();
        }
    }

    // 在保存实体之前，将 List<Long> 转换为逗号分隔的字符串
    @PrePersist
    @PreUpdate
    public void prePersist() {
        if (columnIdsList != null && !columnIdsList.isEmpty()) {
            this.columnIds = columnIdsList.stream()
                    .map(String::valueOf)  // 将 Long 转换为 String
                    .collect(Collectors.joining(","));
        } else {
            this.columnIds = null;
        }
    }

}
